<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Admin extends MY_Controller {

	function __construct()
	{
		parent::__construct();
		$this->load->library('form_validation');
		$this->load->model('poll_model');
		$this->form_validation->set_error_delimiters('<span class="error"></span>');
	}
	
	function index()
	{
		if( ! $this->user_lib->is_admin())
			show_error("Invalid Access", 403);
		else $this->main();
	}
	
	function main()
	{
		if( ! $this->user_lib->is_admin())
			show_error("Invalid Access", 403);
		else $this->load->view('admin/main', $this->data);
	}

	function all_user()
	{
		$this->data->head->title = "전체 사용자 정보";
		$this->data->users = $this->user_model->get_all_users();
		$this->load->view('admin/all_user', $this->data);
	}
	
	function logon_user()
	{
		$sessions = $this->misc_model->get_active_session_from_db();

		$user_ids = array();
		foreach($sessions as $s)
		{
			if(preg_match('/s\:7\:\"user_id\"\;s\:\d+\:\"(?P<user_id>\d+)\"/', $s->user_data, $matches))
			{
				$user_ids[] = $matches['user_id'];
			}
		}
		
		$this->data->users = $this->user_model->get_users_by_ids($user_ids);
		
		$this->load->view('admin/logon_user', $this->data);
	}
	
	function poll_list()
	{
		$this->data->head->title = "설문 보기";
		$this->data->poll_list = $this->poll_model->get_all_polls();
		$this->load->view('admin/poll_list', $this->data);
	}
	
	function high_access()
	{
		$this->data->poll_list = $this->poll_model->high_access_polls();
		$this->load->view('admin/high_access', $this->data);
	}
	
	function location()
	{
		$this->data->head->title = "위치 테이블";
		$this->data->location = $this->misc_model->get_all_location();
		$this->load->view('admin/location', $this->data);
		//print_r($this->data->location);
	}
}
